package cn.xc.controller;

import cn.xc.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@DefaultProperties(defaultFallback ="fallbackMethod")
public class OederController {

    @Autowired
    private RestTemplate restTemplate ;

    @HystrixCommand   //方法熔断
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public User getById(@PathVariable("id")Long id){
        String url = "http://user-server/user/"+id;
        //发送http请求
        return restTemplate.getForObject(url, User.class);
    }

    public User fallbackMethod(@PathVariable("id")Long id){
        return new User(-1L,"熔断了","网络繁忙请稍后再试!");
    }
}
